
class Counter{
    constructor(limit){
        this.limit = limit
    }
    [Symbol.iterator](){
        let count = 1,
        limit = this.limit
        return{
            next(){
                if (count<=limit) {
                    return {done:false,value:count++}
                }else{
                    return {done:true,value:undefined}
                }
            },
            return(){
                console.log('Exit');
                return {done:true}
            }
        }
    }
}

let counter = new Counter(5)
for (const i of counter) {
    if (i>2) {
        break;
    }
    console.log(i);
}
//1
//2
//Exit

let counter2 = new Counter(5)
try {
    for (const i of counter2) {
        if (i>2) {
            throw 'err';
        }
        console.log(i);
    } 
} catch (error) {
    
}
//1
//2
//Exit

let counter3 = new Counter(5)
let {a,b} = counter3

//继续迭代
for (const i of counter2) {
    console.log(i);
} 
//3
//4
//5





